package cn.wolfcode.wolf2w.search.controller;

import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.destination.api.domain.Destination;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.note.api.domain.Note;
import cn.wolfcode.wolf2w.search.api.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import cn.wolfcode.wolf2w.search.service.INoteEsService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SearchController {

    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private INoteEsService noteEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IDestinationEsService destinationEsService;


    @GetMapping("/search")
    public R<?> search(SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.TYPE_STRATEGY: return this.searchStrategy(qo);
            case SearchQueryObject.TYPE_NOTE: return this.searchNote(qo);
            case SearchQueryObject.TYPE_USER: return this.searchUser(qo);
            case SearchQueryObject.TYPE_ANSWER: return this.searchAnswer(qo);
            default: return this.searchAll(qo);
        }
    }

    //攻略搜索
    private R<?> searchStrategy(SearchQueryObject qo) {
        // 全文搜索 + 高亮显示
        Page<Strategy>  page =  strategyEsService.queryByKeyWord(qo);
        return R.ok(page);
    }
    //游记搜索
    private R<?> searchNote(SearchQueryObject qo) {
        // 全文搜索 + 高亮显示
        Page<Note>  page =  noteEsService.queryByKeyWord(qo);
        return R.ok(page);
    }
    //用户搜索
    private R<?> searchUser(SearchQueryObject qo) {
        Page<UserInfo>  page =  userInfoEsService.queryByKeyWord(qo);
        return R.ok(page);
    }
    //文档搜索
    private R<?> searchAnswer(SearchQueryObject qo) {
        return R.ok();
    }
    //所有
    private R<?> searchAll(SearchQueryObject qo) {
        SearchResultVO vo = new SearchResultVO();
        Page<Strategy>  stpage =  strategyEsService.queryByKeyWord(qo);
        Page<Note>  nspage =  noteEsService.queryByKeyWord(qo);
        Page<UserInfo>  uspage =  userInfoEsService.queryByKeyWord(qo);
        Page<Destination>  dspage =  destinationEsService.queryByKeyWord(qo);

        //需求约定，只需要第一页数据即可
        vo.setDests(dspage.getContent());
        vo.setUsers(uspage.getContent());
        vo.setStrategies(stpage.getContent());
        vo.setNotes(nspage.getContent());

        vo.setTotal(stpage.getTotalElements() + nspage.getTotalElements()
                + uspage.getTotalElements() + dspage.getTotalElements());
        return R.ok(vo);
    }




    //当前这个值，需要做到可配置： 缓存到redis做标记
    //将当前这个值， 设置一中可以配置的系统参数
    //private boolean flag = true;

    //一次性接口：逻辑只需要执行一次，后续进来，忽略
    @GetMapping("/dataInit")
    public R<?> dataInit(){
        //初始化-user
        userInfoEsService.initUserInfoEs();
        strategyEsService.initStrategyEs();
        noteEsService.initNoteEs();
        destinationEsService.initDestinationEs();
        return R.ok();
    }
}
